package com.sunshine.lnuplus.view.CountDown;

import android.app.Application;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;

import com.sunshine.lnuplus.utils.DatabaseHelper;

import java.util.ArrayList;
import java.util.List;

public class CountDownViewModel extends AndroidViewModel {
    private static Application application;
    public CountDownViewModel(@NonNull Application application) {
        super(application);
        this.application = application;
        createDatabase();
    }

    private void createDatabase() {
        List<String> databaseList = new ArrayList<>();
        databaseList.add(
                "create table count_down(" +
                        "lesson_name text primary key," +
                        "exam_time text," +
                        "color text," +
                        "exam_location text)"
        );
        DatabaseHelper databaseHelper = new DatabaseHelper(databaseList,getApplication(),"count_down.db",null,1);
        databaseHelper.getWritableDatabase();

        SQLiteDatabase timetableDatabase = DatabaseHelper.getDatabase(getApplication(),"timetable.db");
        Cursor cursor = timetableDatabase.query("timetable",null,null,null,null,null,null);
        if(cursor.moveToFirst()){
            do{
                DatabaseHelper.addCountDownToDB(getApplication(),cursor.getString(cursor.getColumnIndex("cname")),"",cursor.getString(cursor.getColumnIndex("color")),"");
            }while (cursor.moveToNext());
        }
    }

    public static List<List<String>> getLesson() {
        List<List<String>> lists = new ArrayList<>();
        SQLiteDatabase database = DatabaseHelper.getDatabase(application,"count_down.db");
        Cursor cursor = database.query("count_down",null,null,null,null,null,null);
        if(cursor.moveToFirst()){
            do{
                List<String> list = new ArrayList<>();
                list.add(cursor.getString(cursor.getColumnIndex("lesson_name")));
                list.add(cursor.getString(cursor.getColumnIndex("exam_time")));
                list.add(cursor.getString(cursor.getColumnIndex("color")));
                list.add(cursor.getString(cursor.getColumnIndex("exam_location")));
                lists.add(list);
            }while (cursor.moveToNext());
        }
        return lists;
    }
}
